package com.ibeeking.found.log.rest.service.impl;

import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.log.rest.service.ILogCodeGroupService;
import com.ibeeking.found.log.rest.service.ILogGroupService;
import com.ibeeking.found.log.service.mapper.LogGroupMapper;
import com.ibeeking.found.log.service.common.param.LogGroupParam;
import com.ibeeking.found.log.service.common.vo.LogGroupVO;
import com.ibeeking.found.log.service.common.dos.LogGroupDO;
import com.ibeeking.found.log.service.common.query.LogGroupQuery;
import com.ibeeking.found.log.service.common.query.LogGroupPageQuery;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.ParamCheckUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName LogGroupServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-02-25 17:55
 **/
@Service
public class LogGroupServiceImpl extends ServiceImpl<LogGroupMapper, LogGroupDO> implements ILogGroupService {

    @Resource
    private LogGroupMapper logGroupMapper;
    @Resource
    private ILogCodeGroupService logCodeGroupServiceImpl;

    @Override
    public List<LogGroupVO> list(LogGroupQuery logGroupQuery) {
        List<LogGroupDO> logGroupDOs = logGroupMapper.selectList(new LambdaQueryWrapper<LogGroupDO>().like(StringUtils.isNotBlank(logGroupQuery.getName()), LogGroupDO::getName, logGroupQuery.getName()).orderByAsc(LogGroupDO::getSort));
        return JsonUtils.jsonToList(logGroupDOs, LogGroupVO.class);
    }

    @Override
    public Page<LogGroupVO> pageList(LogGroupPageQuery logGroupPageQuery) {
        Page<LogGroupDO> pages = logGroupMapper.selectPage(new Page<LogGroupDO>(logGroupPageQuery.getPageNum(), logGroupPageQuery.getPageSize()), new LambdaQueryWrapper<LogGroupDO>().like(StringUtils.isNotBlank(logGroupPageQuery.getName()), LogGroupDO::getName, logGroupPageQuery.getName()).orderByAsc(LogGroupDO::getSort));
        return BeanUtil.convertPage(pages, LogGroupVO.class);
    }

    @Override
    public Boolean add(LogGroupParam logGroupParam) {
        //id设为空//去除首尾空格
        logGroupParam.setId(null);
        ParamCheckUtils.stringTrim(logGroupParam);
        //校验唯一性
        this.checkUnique(logGroupParam);
        LogGroupDO logGroupDO = BeanUtil.convertBean(logGroupParam, LogGroupDO.class);
        logGroupDO.setCreateBy(UserUtils.getUserId());
        logGroupDO.setCreateTime(LocalDateTime.now());
        logGroupDO.setModifyTime(LocalDateTime.now());
        return logGroupMapper.insert(logGroupDO) > 0;
    }

    @Override
    public Boolean modify(LogGroupParam logGroupParam) {
        ParamCheckUtils.stringTrim(logGroupParam);
        //校验唯一性
        this.checkUnique(logGroupParam);
        LogGroupDO logGroupDO = BeanUtil.convertBean(logGroupParam, LogGroupDO.class);
        logGroupDO.setModifyBy(UserUtils.getUserId());
        logGroupDO.setModifyTime(LocalDateTime.now());
        return logGroupMapper.updateById(logGroupDO) > 0;
    }

    @Override
    public LogGroupVO view(Long id) {
        LogGroupDO logGroupDO = logGroupMapper.selectById(id);
        return BeanUtil.convertBean(logGroupDO, LogGroupVO.class);
    }

    @Override
    public Boolean del(Long id) {
        logCodeGroupServiceImpl.delByGroupId(id);
        return logGroupMapper.deleteById(id) > 0;
    }

    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(LogGroupParam param) {
        //name唯一性
        LambdaQueryWrapper<LogGroupDO> queryWrapper = new LambdaQueryWrapper<LogGroupDO>()
                .ne(param.getId() != null, LogGroupDO::getId, param.getId())
                .eq(LogGroupDO::getName, param.getName());
        List<LogGroupDO> logGroupDOS = logGroupMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(logGroupDOS)) {
            throw BusinessException.create("已经存在同名的日志分组!");
        }
    }
}